package com.dragon.mmochat.message;

import java.awt.Color;
import java.text.DecimalFormat;
import java.util.List;

import com.dragon.mmochat.MmochatConstant;
import com.dragon.mmochat.model.object.MmochatCommonObject;
import com.dragon.mmochat.model.object.MmochatPackageGrid;
import com.dragon.mmochat.model.shop.MmochatPlayerShop;
import com.dragon.mmochat.service.MmochatShopService;
import com.dragon.mmochat.util.MmochatUtil;
import com.skymobi.buffer.SkymobiBuffer;
import com.skymobi.message.SkymobiProtocolMessage;

public class Mmochat_19_6S extends SkymobiProtocolMessage {
	int hallState;
	int curPage;
	MmochatPlayerShop shop = null;

	public Mmochat_19_6S() {
		setMsgCode(MmochatConstant.MSGCODE_MMOCHAT_S_19_6);
	}

	@Override
	protected void encodeBody(SkymobiBuffer out) {
		if (shop == null) {
			return;
		}
		curPage = Math.min(curPage, shop.getPageNum() - 1);
		curPage = Math.max(0, curPage);
		List<MmochatPackageGrid> grids = shop.getGridsByPage(curPage);

		out.putUnsignedShort(hallState);
		out.putInt(shop.getId());
		out.putInt(shop.getShopType().getKey());
		out.putInt(shop.getVersion());
		out.putInt(shop.getState().getKey());
		String baseInfo = "";
		baseInfo += shop.getName() + " ID:" + shop.getId() + "\n";
		baseInfo += "类型：" + shop.getShopType().toString() + " ";
		baseInfo += "状态："
				+ MmochatUtil.wrapColor(shop.getState().toString(),
						Color.yellow) + "\n";
		double shopTax = 100 * MmochatShopService.getShopTax(shop);
		DecimalFormat df = new DecimalFormat("0.0");
		baseInfo += "税率：" + df.format(shopTax) + "% ";
		baseInfo += "信用：" + MmochatShopService.getShopStar(shop) + "\n";
		baseInfo += "每日消耗："
				+ MmochatUtil.getColorMoney(MmochatShopService
						.getShopDailyCostMoney(shop)) + "文钱\n";
		baseInfo += "店内资金：" + MmochatUtil.getColorMoney(shop.getShopMoney())
				+ "文钱\n";
		baseInfo += "当前柜台：" + (curPage + 1) + "/" + shop.getPageNum();

		out.putPrefixedString(baseInfo, 2);
		out.put((byte) curPage);
		out.putShort((short) (curPage * MmochatConstant.gridNumPerShopPage));
		int pos = out.position();
		out.putShort((short) 0);
		int gridNum = 0;
		for (MmochatPackageGrid grid : grids) {
			MmochatCommonObject object = grid.getObject();
			if (object != null) {
				gridNum++;
				out.putInt(grid.getGridIndex());
				out.putUnsignedShort(object.getBmpId());
				out.putPrefixedString(object.getInfo(true), 2);
				out.putShort((short) object.getOverlapNum());
			}
		}
		out.putShort(pos, (short) gridNum);

	}

	public void setHallState(int hallState) {
		this.hallState = hallState;
	}

	public void setCurPage(int curPage) {
		this.curPage = curPage;
	}

	public void setShop(MmochatPlayerShop shop) {
		this.shop = shop;
	}

}
